//
//  YWSearchVideoViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWSearchVideoViewController.h"
#import "LJLTopRadioTVC.h"
#import "LJLRadioPageModel.h"
#import "ZCSPlayMusicViewController.h"

@interface YWSearchVideoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UITableView *radioTableView;
@property (nonatomic, assign) NSInteger maxPage;
@property (nonatomic, strong) NSMutableArray *radioArray;

@end

@implementation YWSearchVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getView];
    [self getData];
}

- (void)getView
{
    [self getAnchorTableView];
}

- (void)getAnchorTableView
{
    self.radioTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30 * FITHEIGHT, SCREENWIDTH, SCREENHEIGHT - 64 - 35) style:UITableViewStylePlain];
    _radioTableView.delegate = self;
    _radioTableView.dataSource = self;
    _radioTableView.separatorStyle = NO;
    [self.view addSubview:_radioTableView];
    
    [_radioTableView registerClass:[LJLTopRadioTVC class] forCellReuseIdentifier:@"radioCell"];
    
    _radioTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        if (_page > _maxPage) {
            [_radioTableView.mj_footer endRefreshingWithNoMoreData];
        }
        else
        {
            [self getDataByPage:_page];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _radioArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100 * FITHEIGHT;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJLTopRadioTVC *radioCell = [tableView dequeueReusableCellWithIdentifier:@"radioCell"];
    radioCell.personL.textColor = [UIColor blackColor];
    radioCell.rnameL.textColor = [UIColor blackColor];
    radioCell.programL.textColor = [UIColor blackColor];
    radioCell.model = [_radioArray objectAtIndex:indexPath.row];
    return radioCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:[self.radioArray objectAtIndex:indexPath.row]];
    ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
    play.isRadio = YES;
    play.musicIndex = 0;
    play.dataArray = array;
    [self presentViewController:play animated:YES completion:nil];
}

- (void)getData
{
    _page = 1;
    self.radioArray = [NSMutableArray array];
    [self getDataByPage:_page];
}

- (void)getDataByPage:(NSInteger)page
{
    NSString *urlString = [NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?condition=relation&core=live&device=iPhone&kw=%@&live=true&page=%ld&paidFilter=false&rows=20", _keyString, page];
    NSLog(@"%@", urlString);
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        NSDictionary *responseDic = [result objectForKey:@"response"];
        NSInteger numFound = [[responseDic objectForKey:@"numFound"] integerValue];
        if (numFound != 0) {
            NSArray *tempArray = [responseDic objectForKey:@"docs"];
            _maxPage = numFound / 20;
            //余数
            NSInteger remainder = numFound % tempArray.count;
            if (remainder != 0) {
                _maxPage++;
            }
            for (NSDictionary *dic in tempArray) {
                LJLRadioPageModel *searchModel = [[LJLRadioPageModel alloc] initWithDic:dic];
                [_radioArray addObject:searchModel];
            }
            [_radioTableView reloadData];
            [_radioTableView.mj_footer endRefreshing];
        }
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
